﻿#include <iostream>
#include <deque>
#include <ctime>
#include <string>
#include <algorithm>

using std::cout;
using std::string;
using std::endl;
using std::deque;


string get_a_target_string()
{
	long target = 0;
	char buf[10];

	cout << "target (0~" << RAND_MAX << "):";
	std::cin >> target;
	snprintf(buf, 10, "%d", target);

	return string(buf);

}

int compareStrings(const void * a, const void * b)
{
	if (*(string*)a > *(string*)b)
		return 1;
	else if (*(string*)a < *(string*)b)
		return -1;
	else
		return 0;
}

void test_deque(long& value)
{
	cout << "\n test_deque .. \n";

deque<string> c;
char buf[10];
clock_t timeStart = clock();

	for (long i = 0; i < value; i++)
	{
		try
		{
			snprintf(buf, 10, "%d", rand());
			c.push_back(string(buf));
		}
		catch (const std::exception& p)
		{
			cout << "i= " << i << " " << p.what() << "\n";
			abort();
		}
	}

	cout << "milli-seconds : " << (clock() - timeStart) << endl;
	cout << "deque.size() = " << c.size() << endl;
	cout << "deque.size() = " << c.max_size() << endl;
	cout << "deque.back() = " << c.back() << endl;
	cout << " deque.front() = " << c.front() << endl;

string target = get_a_target_string();
	timeStart = clock();
auto pItem = std::find(c.begin(), c.end(), target);
	cout << "::find(), milli-seconds: " << (clock() - timeStart) << endl;

	if (pItem != c.end())
	{
		cout << "found, " << *pItem << endl;
	}
	else {
		cout << "not found! " << endl;
	}

	timeStart = clock();
	std::sort(c.begin(), c.end());

	cout << "::sort() , milli-seconds :" << (clock() - timeStart) << endl;
}

int main(void)
{
	long t = 1000000;
	test_deque(t);
	return 0;
}